package io.github.elkan1788.tempmonitor;

import org.nutz.dao.Chain;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.mvc.NutConfig;

/**
 * Some steps on start.
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
public class BootOn implements org.nutz.mvc.Setup {

    @Override
    public void init(NutConfig nc) {
        NutDao dao = nc.getIoc().getByType(NutDao.class);
        dao.execute(Sqls.create("SELECT 'x'"));

        boolean exists = dao.exists(TemperatureMonitorModule.TABLE_NAME);
        if (!exists) {
            String createTableSql = "CREATE TABLE `temperature_monitor` (" +
                    "  `temp_id` int(11) NOT NULL AUTO_INCREMENT," +
                    "  `measure_date` date DEFAULT NULL COMMENT '测量日期'," +
                    "  `temperature` decimal(3,1) DEFAULT NULL COMMENT '体温'," +
                    "  `create_time` datetime DEFAULT NULL COMMENT '测量时间'," +
                    "  PRIMARY KEY (`temp_id`)" +
                    ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";
            dao.execute(Sqls.create(createTableSql));
            dao.execute(Sqls.create("INSERT INTO `temperature_monitor` VALUES " +
                    "('1', '2017-06-04', '36.7', '2017-06-09 13:17:14'), " +
                    "('2', '2017-06-05', '36.4', '2017-06-09 13:18:02'), " +
                    "('3', '2017-06-06', '36.5', '2017-06-09 13:18:23'), " +
                    "('4', '2017-06-07', '36.3', '2017-06-09 13:18:47'), " +
                    "('5', '2017-06-08', '36.5', '2017-06-09 13:19:12'), " +
                    "('6', '2017-06-10', '36.3', '2017-06-10 10:45:04'), " +
                    "('7', '2017-06-11', '36.6', '2017-06-11 09:45:27'), " +
                    "('8', '2017-06-12', '36.3', '2017-06-12 12:32:47'), " +
                    "('9', '2017-06-13', '36.4', '2017-06-13 08:24:17')"));
        }

    }

    @Override
    public void destroy(NutConfig nc) {

    }
}
